home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / GNUST / !GNUst / st / String < prev    next >
Text File  |  1991-09-13  |  6KB  |  246 lines

  1. "======================================================================
  2. |
  3. |   String Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         12 Sep 91      Fixed second fileName method definition to be
  34. |              filePos.
  35. |
  36. | sbyrne      3 Sep 89      added asString for method source package
  37. |
  38. | sbyrne     25 Apr 89      created.
  39. |
  40. "
  41.  
  42. ArrayedCollection variableByteSubclass: #String
  43.           instanceVariableNames: ''
  44.           classVariableNames: ''
  45.           poolDictionaries: ''
  46.           category: nil.
  47.  
  48. String comment: 
  49. 'My instances represent string data types.  I provide accessing and
  50. manipulation methods for string data types.' !
  51.  
  52. !String class methodsFor: 'basic'!
  53.  
  54. fromString: aString
  55.     " ### probably should use regular copy"
  56.     ^aString copyFrom: 1 to: aString size
  57. !
  58.  
  59. readFrom: aStream
  60.     | str newChar |
  61.     str _ WriteStream on: (String new: 0).
  62.     aStream do:            " ### Is this risky, doing a next inside? "
  63.         [ :char | char == $'
  64.                   ifTrue: [ newChar _ aStream next.
  65.                           newChar == $' ifFalse: [ str nextPut: $' ].
  66.                 str nextPut: newChar ]
  67.                       ifFalse: [ str nextPut: char ]
  68.     ].
  69.     ^str contents
  70. !!
  71.  
  72.  
  73.  
  74. !String methodsFor: 'comparing'!
  75.  
  76. < aString
  77.     "Return true if the receiver is less than aString, ignoring case
  78.     differences."
  79.     self >= aString ifTrue: [ ^false ]
  80.                     ifFalse: [ ^true ]
  81. !
  82.  
  83. > aString
  84.     "Return true if the receiver is greater than aString, ignoring case
  85.     differences."
  86.     self <= aString ifTrue: [ ^false ]
  87.                     ifFalse: [ ^true ]
  88. !
  89.  
  90. <= aString
  91.     "Returns true if the receiver is less than or equal to aString,
  92.     ignoring case differences.  If is receiver is an initial substring of
  93.     aString, it is considered to be less than aString."
  94.     | c1 c2 |
  95.     " Scan self and aString until a character is clearly greater or lesser
  96.       (All preceding characters must have been equal).  If the end is reached,
  97.       one of the strings is a possibly improper initial substring of the other,
  98.       and for the receiver to be less than aString, it must be the initial
  99.       substring."
  100.     1 to: (self size min: aString size) do:
  101.         [ :i | c1 _ (self at: i) asLowercase.
  102.            c2 _ (aString at: i) asLowercase.
  103.            c1 < c2 ifTrue: [ ^true ].
  104.            c1 > c2 ifTrue: [ ^false ] ].
  105.     ^self size <= aString size
  106. !
  107.  
  108. >= aString
  109.     "Returns true if the receiver is greater than or equal to aString,
  110.     ignoring case differences.  If is aString is an initial substring of
  111.     the receiver, it is considered to be less than the receiver."
  112.     | c1 c2 |
  113.     1 to: (self size min: aString size) do:
  114.         [ :i | c1 _ (self at: i) asLowercase.
  115.            c2 _ (aString at: i) asLowercase.
  116.            c1 < c2 ifTrue: [ ^false ].
  117.            c1 > c2 ifTrue: [ ^true ] ].
  118.     ^self size >= aString size
  119. !
  120.  
  121.  
  122.  
  123. sameAs: aString
  124.     "Returns true if the receiver is the same string as aString, ignoring
  125.     case differences."
  126.     self size ~= aString size ifTrue: [ ^false ].
  127.     1 to: self size do:
  128.         [ :i | (self at: i) asLowercase ~= (aString at: i) asLowercase
  129.             ifTrue: [ ^false ] ].
  130.     ^true
  131. !
  132.  
  133.  
  134. match: aString
  135.     ^self asLowercase matchSubstring: 1 in: aString asLowercase at: 1
  136. !!
  137.  
  138.  
  139.  
  140. !String methodsFor: 'converting'!
  141.  
  142. asUppercase
  143.     "Returns a copy of self as an uppercase string"
  144.     | newStr |
  145.     newStr _ self species new: self size.
  146.     1 to: self size do:
  147.         [ :i | newStr at: i put: (self at: i) asUppercase ].
  148.     ^newStr
  149. !
  150.  
  151. asLowercase
  152.     "Returns a copy of self as a lowercase string"
  153.     | newStr |
  154.     newStr _ self species new: self size.
  155.     1 to: self size do:
  156.         [ :i | newStr at: i put: (self at: i) asLowercase ].
  157.     ^newStr
  158. !
  159.  
  160. asString
  161.     "But I already am a string!  Really!"
  162.     ^self
  163. !
  164.  
  165. fileName
  166.     "But I don't HAVE a file name!"
  167.     ^nil
  168. !
  169.  
  170. filePos
  171.     "But I don't HAVE a file position!"
  172.     ^nil
  173. !
  174.  
  175. asSymbol
  176.     "Returns the symbol corresponding to the string"
  177.     ^Symbol intern: self
  178. !!
  179.  
  180.  
  181.  
  182. !String methodsFor: 'copying'!
  183.  
  184. shallowCopy
  185.     | newStr |
  186.     newStr _ self species new: self size.
  187.     1 to: self size do:
  188.         [ :i | newStr at: i put: (self at: i) ].
  189.     ^newStr
  190. !
  191.  
  192. deepCopy
  193.     ^self shallowCopy
  194. !!
  195.  
  196.  
  197.  
  198.  
  199. !String methodsFor: 'printing'!
  200.  
  201. printOn: aStream
  202.     ^self do: 
  203.     [ :char | aStream nextPut: char ] "this seems too primitive"
  204. !!
  205.  
  206.  
  207.  
  208. !String methodsFor: 'storing'!
  209.  
  210. storeOn: aStream
  211.     aStream nextPut: $'.
  212.     self do:
  213.         [ :char | char == $' ifTrue: [ aStream nextPut: char ].
  214.               aStream nextPut: char ].
  215.     aStream nextPut: $'
  216. !!
  217.  
  218.  
  219.  
  220. !String methodsFor: 'private'!
  221.  
  222. matchSubstring: p in: aString at: s
  223.     | pc |
  224.     p > self size
  225.         ifTrue: [ ^s > aString size ].
  226.     pc _ self at: p.
  227.     pc = $*
  228.         ifTrue: [ s to: (aString size) + 1 do:
  229.                 [ :ss | (self matchSubstring: p + 1
  230.                           in: aString
  231.                   at: ss)
  232.                     ifTrue: [ ^true ] ].
  233.           ^false ].
  234.     s > aString size ifTrue: [ ^false ].
  235.     pc = $#
  236.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  237.  
  238.     pc = (aString at: s)
  239.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  240.     ^false
  241. !!
  242.  
  243.  
  244.